# V Final Exhibit - Table 2 -  V2 : Average Monthly Spending by Eventual Outcome --------



DT.for.set.nms   <- function(x,vars) { 
  for (j in vars)  set(x,which(is.na(x[[j]])),j,0) 
}

reweighting_categories_all <- function(data,
                                       data_pop,
                                       categories,
                                       parm_month = 30,
                                       parm_bins = 0.1) {
  
  
  dt_cost_cancer_wide_prob <- merge(x = data_pop[,
                                                 c("id_var","DMG_age","S_index_date_XX",
                                                   "prob_for_report","DMG_date_of_death_XX",
                                                   "DMG_died_within_365d")],
                                    y = data,
                                    by = c("id_var","S_index_date_XX"),
                                    all.x = TRUE,
                                    all.y = FALSE)
  
  
  DT.for.set.nms(dt_cost_cancer_wide_prob,
                 categories)
  
  
  dt_cost_cancer_wide_prob[,days_lived := ifelse(DMG_died_within_365d=="1",
                                                 as.numeric(difftime(DMG_date_of_death_XX,
                                                                     S_index_date_XX , 
                                                                     units = "days"))+1 ,
                                                 366)]
  
  dt_cost_cancer_long_prob <- melt.data.table(dt_cost_cancer_wide_prob,
                                              id.vars = c("id_var",
                                                          "prob_for_report","DMG_died_within_365d",
                                                          "days_lived"),
                                              measure.vars = categories) 
  
  dt_cost_by_p_death_cat <- 
    dt_cost_cancer_long_prob[,.( value                 =sum( value )/sum(days_lived)*parm_month,
                                 sum_value             = sum(value),
                                 sum_days              = sum(days_lived),
                                 N=.N),
                             by= .(bins = plyr::round_any(prob_for_report, parm_bins, floor),
                                   group=as.factor(ifelse(DMG_died_within_365d=="1","Decedent","Survivor")),
                                   variable)][order(bins)]
  
  
  ggplot(data = dt_cost_by_p_death_cat ,
         aes(x=bins,color = group, y = value ))+
    geom_line()+
    facet_grid(~variable)
  
  
  dt_cost_by_p_death_cat[,`:=`(w_days = sum_days/sum(sum_days),
                               w_N =    N/sum(N)),
                         by = .(group,variable)]
  
  
  dt_cost_by_p_death_cat_W <- merge (x= dt_cost_by_p_death_cat,
                                     y= dt_cost_by_p_death_cat[group == "Decedent",.(bins,
                                                                                     variable,
                                                                                     w_days_D = w_days,
                                                                                     w_N_D = w_N)],
                                     by = c("bins","variable") ,
                                     all.x = T,
                                     all.y = F )
  
  
  dyn_wgt_cost <-  dt_cost_by_p_death_cat_W[,.(#cost_days = weighted.mean(cost,w_days_D),
    cost = weighted.mean(value,w_N_D) ),
    by = .(group,variable)]
  
  
  dt_cost_by_dyn_prob_death_all_cat <- 
    dt_cost_cancer_long_prob[,.(value=sum(value)/sum(days_lived)*parm_month,
                                sum_value= sum(value),
                                sum_days = sum(days_lived),
                                N=.N),
                             by= .(variable ,
                                   group=as.factor(ifelse(DMG_died_within_365d=="1","Decedent","Survivor")))]
  
  Table2_dyn_cat <- 
    (data.table( Category  = categories,   
                 Survivor                      = as.numeric(dt_cost_by_dyn_prob_death_all_cat[group=="Survivor",value] ),
                 Survivor_p0_reweigthed        = as.numeric(dyn_wgt_cost[group=="Survivor",cost]),
                 Decedent_all                  = as.numeric(dt_cost_by_dyn_prob_death_all_cat[group=="Decedent",value])
    ))
  
  
  Table2_dyn_cat[,Difference := Decedent_all - Survivor_p0_reweigthed  ]
  
  Table2_dyn_cat[,type:=ifelse(Category=="Others",1,0)][order(type,-Difference)]
  Table2_dyn_cat[,type:=NULL]
  
}





combine_table_2 <- function(results_I ,
                            results_P,
                            file_name_t , 
                            hospital_a  = "Inpatient_Unplanned",
                            hospital_b   ="Inpatient_Planned") {
  
  intensity_cat <- c("High","Low")
  res_temp<- rbind( data.table(t(colSums(results_I[,-1])))[,Category:="Total"],
                    data.table(t(colSums(results_I[Category%in%intensity_cat,-1])))[,Category:="All Inpatient:"],
                    results_P[ Category%in%c(hospital_a)][,Category := gsub("Inpatient_","", Category)],
                    results_P[ Category%in%c(hospital_b)][,Category := gsub("Inpatient_","", Category)],
                    results_I[Category%in%intensity_cat,][,Category :=
                                                            paste0(Category," Intensity")][order(-Difference)],                                                                                    
                    
                    data.table(t(colSums( results_I[!Category%in%intensity_cat,-1])))[,Category:="Other Services:"],
                    results_I[!Category%in%intensity_cat][,
                                                          type := ifelse(Category=="Others",0,1)][order(-type,-Difference)][,type:=NULL][Category=="Others",Category:="Other"]
  )
  
  res_temp[,sh_total := format(round((Difference/colSums(results_I[,5]))*100 ,1 ),nsmall = 1)]
  
  setcolorder(res_temp,"Category")
  
  res_temp[, seq(2,5) := 
             lapply(.SD,scales::comma,accuracy=1), 
           .SDcols = seq(2,5)]
  
  res_temp$tot<- c(T,T,F,F,F,F,T,F,F,F,F)
  
  res_temp[, sh_total := ifelse(tot==T,
                                paste0(sh_total),
                                paste0(sh_total,"~~~"))]
  
  res_temp[,Category:= ifelse(tot==T,
                              Category,
                              paste0("~~",Category))]
  res_temp[,tot:=NULL]
  
  invisible(Hmisc::latex(
    res_temp,
    file = paste0(file_name_t),
    center = 'centering',
    n.cgroup = c(1,2, 1 ,2 ),
    cgroup   = c("","Survivor", "Decedent", "Difference"),
    extracolheads = c("","(1)", "(2)", "(3)", "(4)","(5)"),
    colheads = c("\\thead{Category}",
                 "\\thead{Unweighted}",
                 "\\thead{Reweighted by\\\\Decedent Risk}", 
                 "\\thead{Adjusted for \\\\ Survival\\\\Duration}",
                 "\\thead{Decedent -\\\\ Survivor\\\\(Reweighted)}",
                 "\\thead{Percent of \\\\ Total Difference}"),
    rowname =NULL,
    col.just = c("l", rep.int("r", 5)), 
    extracolsize = "normalsize"
  ))
  
  res_temp
}




do_table_2<- function(data = dt_for_exhibits_cancer ,
                      data_cost = dt_cost_for_exhibits_cancer, 
                      intensity_wards = c("oncology","internal_medicine",
                                          "geriatry","rehabilitation"),
                      sample = "cancer",
                      hosp_a = "Inpatient_Unplanned",
                      hosp_b = "Inpatient_Planned") {
  
  top_categories <- unique(data_cost[!is.na(main_cat)]$main_cat)
  # first version - main cat - planned/ unplanned
  dt_cost_cancer_wide<- dcast.data.table(data_cost,
                                         id_var  +  S_index_date_XX  ~ main_cat ,
                                         value.var = c("actual_cost") , 
                                         fun.aggregate = sum)
  
  top_categories <- names(dt_cost_cancer_wide[,4:9])
  
  
  top_cat_table_2_ALL <- reweighting_categories_all(dt_cost_cancer_wide,
                                                    data,
                                                    top_categories,
                                                    31,0.1)
  
  # Second version - main cat - Intensity 
  dt_cost_cancer_wide_Intensity<- dcast.data.table(data_cost[
    ][,new_cat := as.character(main_cat)][
      main_cat%in%c(hosp_a,hosp_b),
      new_cat:=factor(ifelse(profession%in%intensity_wards,
                             "Low","High")) ],
    id_var  +  S_index_date_XX  ~ new_cat ,
    value.var = c("actual_cost") ,
    fun.aggregate = sum)
  
  top_categories_inten<- names(dt_cost_cancer_wide_Intensity[,4:9])
  
  top_cat_table_2_ALL_Intensity <- reweighting_categories_all(dt_cost_cancer_wide_Intensity,
                                                              data,
                                                              top_categories_inten,
                                                              31,0.1)
  hosp_A1<- hosp_a
  hosp_B1<- hosp_b
  
  rrrr<-combine_table_2(results_I = top_cat_table_2_ALL_Intensity,
                        results_P = top_cat_table_2_ALL,
                        file_name_t = paste0("ave_monthly_cost_table2_",sample,"_func.tex"),
                        hospital_a = hosp_A1,
                        hospital_b = hosp_B1)
  
  rrrr
}














